package com.jdd.pedicure.controller;


import com.jdd.pedicure.entity.*;
import com.jdd.pedicure.service.OrderService;
import com.jdd.pedicure.service.RoomService;
import com.jdd.pedicure.service.StaffService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RequestMapping("orders")
@Controller
public class OrderController {
    @Resource
    OrderService orderService;
    @Resource
    RoomService roomService;
    @Resource(name = "staffservice")
    StaffService staffService;

    @RequestMapping("goAdd")
    public String goAdd() {
        return "orderAdd";
    }

    @RequestMapping("goOrders")
    public String goOrder() {
        return "order";
    }

    @RequestMapping("getbest")
    @ResponseBody
    public Result getbest() {
        String message = orderService.getbest();
        String item = message.split(",")[0];
        String name = message.split(",")[2];
        List<String> mess = new ArrayList<>();
        mess.add(item);
        mess.add(name);
        Result result = new Result();
        result.setCode("0");
        result.setMsg("成功");
        result.setData(mess);
        return result;
    }
    /*
        @RequestMapping("goAdd")
        public String goAdd(Integer id, HttpServletRequest request){
            Orders o=new Orders();
            if(id!=null){
                o=orderService.queryById(id);
            }
            request.setAttribute("order",o);
            return "orderAdd";
        }*/
    @RequestMapping("getData")
    @ResponseBody
    public Result getData(Result<Orders> result, Orders example) {
        result.setExample(example);
        orderService.queryByPage(result);
        result.setCode("0");
        result.setMsg("查询成功");
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(int id) {
        orderService.delete(id);
        Result result = new Result();
        result.setCode("0");
        result.setMsg("删除成功");
        return result;
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(@RequestBody OrderSave orderSave, HttpSession session) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Result result = new Result();
        Orders orders = new Orders();
        orders.setAdminName((String) session.getAttribute("username"));
        orders.setOrderTime(sdf.format(new Date()));
        orders.setCustomerName(orderSave.getCustomerName());
        orders.setStaffId(orderSave.getStaffId());
        orders.setOrderState(orderSave.getOrderState());
        Staff staff = staffService.queryById(Integer.parseInt(orderSave.getStaffId()));
        if (!staff.getStaffState().equals("1")) {
            result.setCode("1");
            result.setMsg("员工不是空闲状态");
            return result;
        }
        orders.setStaffName(staff.getStaffName());
        staff.setStaffState("0");
        String itemId = "";
        Integer money = 0;
        Room room = roomService.queryById(Integer.parseInt(orderSave.getRoomId()));
        if (!room.getRoomState().equals("0")) {
            result.setCode("1");
            result.setMsg("改房间已经被使用");
            return result;
        }
        money += room.getRoomPrice();
        orders.setRoomId(room.getRoomId() + "");
        for (Item item : orderSave.getItems()) {
            itemId += item.getItemName() + ",";
            money += item.getItemPrice();
        }
        orders.setRoomName(room.getRoomName());
        orders.setOrderMoney(money);
        orders.setOrderItems(itemId);
        room.setRoomState("1");
        roomService.saveOrUpdate(room);
        orderService.saveOrUpdate(orders);
        staffService.saveOrUpdate(staff);
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }

    @RequestMapping("pay")
    @ResponseBody
    public Result pay(Integer id) {
        Orders orders = orderService.queryById(id);
        orders.setOrderState("1");
        orderService.saveOrUpdate(orders);
        Staff staff = staffService.queryById(Integer.parseInt(orders.getStaffId()));
        staff.setStaffState("1");
        Room room = roomService.queryById(Integer.parseInt(orders.getRoomId()));
        room.setRoomState("0");
        roomService.saveOrUpdate(room);
        staffService.saveOrUpdate(staff);
        Result result = new Result();
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }

}
